<?php
/**
 * Created by akon
 * Date: 2016/12/4 0004
 * Time: 22:10
 * 菜单表操作模型
 */
namespace Manager\Model;
use Think\Model;
class MenuModel extends Model{
    /**菜单数据添加**/
    public function menuAdd($data){
        $res = $this->add($data);
        return $res;
    }
    /**查询菜单数据**/
    public function getMenu(){
        $res = $this->where("type=1")->order("id desc")->select();
        return $res;
    }
    /**查询当前菜单项**/
    public function getCurrentMenu($id){
        $res = $this->where(array("id"=>$id))->find();
        return $res;
    }
    /**编辑当前数据**/
    public function editCurrentMenu($data,$parent_id){
        //改变父级分类，如果当前菜单有子级分类，则获得当前parent_id,将其赋给子级分类
        $this->where(array('parent_id'=>$data['id']))->setField("parent_id",$parent_id);
        $res = $this->save($data);
        return $res;
    }
    /**删除菜单**/
    public function delMenu($id){
        //1、获得当前菜单的父级id
        $parent_id = $this->where("id=" . $id)->field("parent_id")->find();
        //2、更新子级的父级id
        $this->where("parent_id=" . $id)->setField("parent_id",$parent_id['parent_id']);
        $res = $this->where("id=" . $id)->setField("type",0);
        return $res;
    }

    /**递归获得所有子级的id
     * @param $data  所有数据
     * @param $id   当前分类id
     */
    public function getSonId($data,$id){
        //定义空数组，用来存放子级id
        $temp = array();
        foreach($data as $v){
            if($v['parent_id'] == $id){
                $temp[] = $v['id'];
                array_merge($temp,$this->getSonId($data,$v['id']));
            }
        }
        return $temp;
    }
    /**获得除了自己和子级的所有分类**/
    public function getNoMy($id){
        //获得所有子级id
        $idArr = $this->getSonId($this->select(),$id);
        //将自己压入数据
        $idArr[] = $id;
        $idStr = implode(',',$idArr);
        $info = $this->where("id NOT IN($idStr)")->select();
        return $info;
    }

    /**批量开启选中项**/
    public function batchOpening($id){
        $res = $this->where("id in($id)")->setField("is_open",1);
        return $res;
    }
    /**批量取消**/
    public function batchCancel($id){
        $res = $this->where("id in($id)")->setField("is_open",0);
        return $res;
    }
    /**批量删除**/
    public function batchDel($id){
        $res = $this->where("id in($id)")->setField("type",0);
        return $res;
    }
}